package com.tkmybatis.hfy.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

import org.apache.commons.codec.binary.Base64;

import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;

/**
 * 图片处理工具
 * @author ywt
 *
 */
@Slf4j
public class ImageUtil {
    
    public static void proportionImage(float scale, File oldImg, File newImg) {
        try {
            Thumbnails.of(oldImg)   
            .scale(scale)  
            .toFile(newImg);
        } catch (Exception e) {
            log.error("error:",e);
        }
    } 
    
    public static String ImageToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            log.error("error:",e);
        }
        // 对字节数组Base64编码
        Base64 base64 = new Base64();
        // 返回Base64编码过的字节数组字符串
        return base64.encodeToString(data);
    }
}
